﻿#include "regionalpins.h"

#include <QApplication>
#include <QFile>
#include <QDebug>

RegionalPins  * RegionalPins::m_regionalPins = NULL;
RegionalPins::RegionalPins(QObject *parent) : QObject(parent)
{
    initPin();
}

RegionalPins::~RegionalPins()
{

}

void RegionalPins::initPin()
{
    m_regionalNumbers.clear();
    m_regionalTimers.clear();
    QString docParhFile =  QApplication::applicationDirPath();
    docParhFile +="/configs/regionalpins.json";

    QFile files(docParhFile);
    if(files.open(QIODevice::ReadOnly)){
        QString inforsa = files.readAll();
        QJsonParseError errors;
        QJsonDocument doc = QJsonDocument::fromJson(inforsa.toUtf8(),&errors);
        if(errors.error == QJsonParseError::NoError){
            QJsonArray arrya = doc.array();
            for(int i = 0 ; i < arrya.size() ;i++ ){
                QJsonObject itema = arrya.at(i).toObject();
                QString regional =  itema.take("regional").toString();
                 int times =  itema.take("times").toInt();
                QJsonArray arrysa = itema.take("pins").toArray();
                QSet<int> lista;
                for(int j = 0 ; j < arrysa.size();j++){
                    QJsonObject itemb = arrysa.at(j).toObject();
                    int pina =  itemb.take("pin").toInt();
                    lista.insert(pina);

                }
                m_regionalNumbers.insert(regional,lista);
                m_regionalTimers.insert(regional,times);
            }
        }
        files.close();
    }
#ifdef DEBUG_INFOR
     qDebug()<<__func__<<__LINE__<<m_regionalNumbers;
#endif
}

RegionalPins *RegionalPins::gestance()
{
    if(m_regionalPins == NULL){
        m_regionalPins = new RegionalPins();
    }

    return m_regionalPins;
}

QSet<int> RegionalPins::getPins(QString regins)
{

    return m_regionalNumbers.value(regins);
}

int RegionalPins::getRegionalTimers(QString regins)
{
    return m_regionalTimers.value(regins);
}
